package com.ants.platform.core.servlet;

import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.View;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;
import java.util.Map;

/**
 * 重写springmvc的部分方法
 * 解决：当视图找不到的时候页面会报500错误
 * Created by liushun on 2016/8/11.
 */
public class MyDispatcherServlet extends DispatcherServlet {

    //解决：当视图找不到的时候页面会报500错误
    @Override
    protected View resolveViewName(String viewName, Map<String, Object> model, Locale locale, HttpServletRequest request) throws Exception {
        //每个页面放入网站路径
        request.setAttribute("ctx", request.getContextPath());
        View view = super.resolveViewName(viewName, model, locale, request);
        if (view == null) {
            model.put("message", viewName + "模板文件没有找到");
            return super.resolveViewName("error/exception", model, locale, request);
        }
        return view;
    }
}
